-- UIInstanceEnter
-- Created by chengb Apr/14/2015
-- 副本进入界面

-- UIInstanceEnter 继承自Layer
UIInstanceEnter = class("UIInstanceEnter", function()
    return cc.Layer:create();
end);

function UIInstanceEnter.create(pos, instanceId, okFunc, okText)
    return UIInstanceEnter.new(pos, instanceId, okFunc, okText);
end

-- 构造函数
function UIInstanceEnter:ctor(pos, instanceId, okFunc, okText)
    self:setName("UIInstanceEnter");

    -- 初始化
    local node = cc.CSLoader:createNode("layout/dungeon/BuildingView.csb");
    self:addChild(node);
    self.node = node;

    self:setLocalZOrder(UIMgr.TOP_MENU_ZORDER + 1);

    -- 记录副本id
    self.pos = pos;
    self.instanceId = instanceId;

    -- 记录下传入数据
    self.okFunc = okFunc;
    self.okText = okText;

    -- 注册事件处理回调函数
    self:registerEventCallback()

    -- 注册点击事件
    self:registerTouchEvent();

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();
end

-- 适配
function UIInstanceEnter:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

-- 注册事件处理回调函数
function UIInstanceEnter:registerEventCallback()
    self:registerScriptHandler(function(eventType)
        if eventType == "exit" then
            cclog("UIInstanceEnter 界面析构清理");
            if UIDungeonMgr:getCurLevel() then
                UIDungeonMgr:getCurLevel():showMenu();
            end

        elseif eventType == "enter" then
            if UIDungeonMgr:getCurLevel() then
                UIDungeonMgr:getCurLevel():hideMenu();
            end
        end
    end);
end

-- 注册点击事件
function UIInstanceEnter:registerTouchEvent()
    local node = self.node;

    -- 注册返回按钮点击事件
    local btnBack = findChildByName(node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_return");
            -- 关闭当前界面
            UIMgr.getCurrentScene():removeFormByName("UIInstanceEnter");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    -- 注册确定按钮点击事件
    local btnOk = findChildByName(node, "CT/btn_ok");
    local function onOkClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_select");

            if self.okFunc then
                self:okFunc();
            else
                -- 尝试进入
                if self:tryStart() then
                    -- 关闭当前界面
                    UIMgr.getCurrentScene():removeFormByName("UIInstanceEnter");
                end
            end
        end
    end
    btnOk:addTouchEventListener(onOkClick);
    TextStyleM.setTextStyle(btnOk, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);

    -- 确定按钮文本
    local text = self.okText and self.okText or getLocStr("btn_text_enter");
    btnOk:setTitleText(text);
end

-- 尝试进入副本
function UIInstanceEnter:tryStart()
    -- 尝试进入副本
    if not DungeonInstanceM.canStartInstance(self.instanceId) then
        -- 不满足进入条件
        return false;
    end

    -- 关闭地牢主界面
    UIMgr.getCurrentScene():removeFormByName("UIDungeonInfo");
    UIMgr.getCurrentScene():removeFormByName("UIDungeonTopMenu");

    -- 进入副本
    return DungeonActionM.go("start_instance", self.pos, self.instanceId);
end

-- 重绘
function UIInstanceEnter:redraw()
    local CT = findChildByName(self.node, "CT");
    local BT = findChildByName(self.node, "BT");

    local instanceInfo = DungeonInstanceM.getInstanceInfo(self.instanceId);

    -- 显示标题
    local kerning, kerning2 = 0, 0;
    if getLang() ~= "zh" and getLang() ~= "tw" then
        kerning, kerning2 = -3, -1;
    end

    local titleLabel = findChildByName(CT, "title_node/title");
    TextStyleM.setTextStyle(titleLabel, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true, kerning);
    titleLabel:setString(instanceInfo["name"])
    TextStyleM.setArtWordTitleStyle(titleLabel);

    -- 显示建筑图标
    local iconImg = findChildByName(self.node, "CT/icon");
    iconImg:loadTexture(getLevelItemIconPath(instanceInfo["icon"]));
    iconImg:setScale(1.3);

    -- 显示功能描述
    local desc = instanceInfo["desc"];
    local tipLabel = findChildByName(CT, "desc");
    TextStyleM.setTextStyle(tipLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN, false, kerning2);
    tipLabel:setString(desc);
end
